package ModelLayer;

import java.text.DecimalFormat;

public class SaleItem extends Item {

	private static final long serialVersionUID = 1L;
	private static long barcodeSeed = 0;
	private String barcode;
	private double bPrice;
	private double sPrice;
	private double tax;
	private String um;
	private int minStock;
	private int maxStock;
	private int currStock;
	private int discountQ;
	private Contractor contractor;

	public SaleItem() {
		super();
		this.bPrice = 0;
		this.sPrice = 0;
		this.tax = 0;
		this.um = "";
		this.minStock = 0;
		this.maxStock = 0;
		this.currStock = 0;
		this.discountQ = 0;
		setBarcode(generateBarcode());
	}

	public SaleItem(String name, int location, double bPrice, double sPrice,
			double tax, String um, int minStock, int maxStock, int currStock,
			int discountQ, int contractorID) throws RealException {
		super(name, location);
		this.bPrice = bPrice;
		this.sPrice = sPrice;
		this.tax = tax;
		this.um = um;
		this.minStock = minStock;
		this.maxStock = maxStock;
		this.currStock = currStock;
		this.discountQ = discountQ;
		this.contractor = ContractorRecords.getInstance().getContractor(
				contractorID);
		setBarcode(generateBarcode());
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public double getbPrice() {
		return bPrice;
	}

	public void setbPrice(double bPrice) {
		this.bPrice = bPrice;
	}

	public double getsPrice() {
		return sPrice;
	}

	public void setsPrice(double sPrice) {
		this.sPrice = sPrice;
	}

	public double getTax() {
		return tax;
	}

	public void setTax(double tax) {
		this.tax = tax;
	}

	public String getUm() {
		return um;
	}

	public void setUm(String um) {
		this.um = um;
	}

	public int getMinStock() {
		return minStock;
	}

	public void setMinStock(int minStock) {
		this.minStock = minStock;
	}

	public int getMaxStock() {
		return maxStock;
	}

	public void setMaxStock(int maxStock) {
		this.maxStock = maxStock;
	}

	public int getCurrStock() {
		return currStock;
	}

	public void setCurrStock(int currStock) {
		this.currStock = currStock;
	}

	public int getDiscountQ() {
		return discountQ;
	}

	public void setDiscountQ(int discountQ) {
		this.discountQ = discountQ;
	}

	public Contractor getContractor() {
		return contractor;
	}

	public void setContractor(Contractor contractor) {
		this.contractor = contractor;
	}

	public void setContractor(int contractorID) throws RealException {
		this.contractor = ContractorRecords.getInstance().getContractor(
				contractorID);
	}

	private static String generateBarcode() {
		// creating a barcode number with padding 0 on left, actually a String
		barcodeSeed++;
		String barcode = String.format("%012d", barcodeSeed);
		return barcode;
	}

	public static long getLastBarcodeSeed() {
		return barcodeSeed;
	}

	public static void setLastBarcodeSeed(long barcodeseed) {
		barcodeSeed = barcodeseed;
	}

	@Override
	public String toString() {
		return "Barcode: " + getBarcode() + ", name: " + getName() + ", location: " + getLocation() + ", buyPrice: " + getbPrice()
				+ ", sellPrice: " + getsPrice() + ", tax: " + Double.valueOf(new DecimalFormat("#.##").format(getTax() * 100)) + ", um: " + getUm()
				+ ", minStock: " + getMinStock() + ", maxStock: " + getMaxStock()
				+ ", currStock: " + getCurrStock() + ", discountQ: " + getDiscountQ()
				+ ", contractor id: " + getContractor().getId();
	}
}
